using System;
using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;
using I2.Loc;
using UnityEngine;

public class ReputationInterfaceController : MonoBehaviour
{
	[Serializable]
	public struct MapDistrictSettings
	{
		public string sceneName;

		public GameObject districtAnchor;

		public GameObject districtBackground;
	}

	public static ReputationInterfaceController instance;

	[SerializeField]
	private float zoomOnAnimationFactor = 0.95f;

	[Header("Map")]
	public GameObject mapBackground;

	public BoxCollider mapBackgroundCollider;

	[SerializeField]
	private UIScrollView scrollView;

	[SerializeField]
	private UIPanel panel;

	[SerializeField]
	private UICenterOnChild centerOnChild;

	[SerializeField]
	private Transform centerMap;

	[SerializeField]
	private Transform map;

	[Header("TopPanel")]
	[SerializeField]
	private UISprite levelBar;

	[SerializeField]
	private UILabel levelLable;

	[SerializeField]
	private UILabel cashCount;

	[SerializeField]
	private UILabel goldCount;

	[SerializeField]
	private UILabel capturedCount;

	[HideInInspector]
	public List<MapDistrict> districsList = new List<MapDistrict>();

	[HideInInspector]
	public Dictionary<string, ReputationMapInfo> districsInfos = new Dictionary<string, ReputationMapInfo>();

	public List<MapDistrictSettings> districtsSetting = new List<MapDistrictSettings>();

	private Tweener tween;

	private Transform curCenter;

	private Camera _camera;

	public GameObject connect;

	public GameObject disconnect;

	public GameObject messageNoInternet;

	public GameObject messageMapUnAvailable;

	private bool _changeDistrictState;

	public IEnumerator cns;

	private bool isZoomOut;

	public static event Action<bool> UpdateInfo;

	public static event Action<bool> ShowStats;

	public static event Action ShowNewCaptured;

	public static event Action<bool> networkAvailable;

	private void Awake()
	{
		instance = this;
		HOTween.Init();
		_camera = Camera.main;
		MapDistrict.EndOfChangeAmountReputationAnimation += EndOfReputationAnimationEventHandler;
		MapDistrict.EndOfAllAnimations += ZoomIn;
		MapDistrict.StartAnimations += ZoomOut;
		mapBackgroundCollider.enabled = false;
		if (connect != null)
		{
			connect.SetActive(false);
		}
		if (disconnect != null)
		{
			disconnect.SetActive(false);
		}
		if (messageNoInternet != null)
		{
			messageNoInternet.SetActive(false);
		}
		InitMap();
	}

	private void InitMap()
	{
		GameObject original = Resources.Load("ReputationMap/DistrictExample") as GameObject;
		foreach (MapDistrictSettings item in districtsSetting)
		{
			GameObject gameObject = UnityEngine.Object.Instantiate(original, Vector3.zero, Quaternion.identity) as GameObject;
			gameObject.name = item.sceneName;
			gameObject.transform.parent = map;
			gameObject.transform.localScale = Vector3.one * 0.7f;
			gameObject.transform.position = item.districtAnchor.transform.position;
			MapDistrict component = gameObject.GetComponent<MapDistrict>();
			component.nameScene = item.sceneName;
			component.districtButton = item.districtBackground.GetComponent<UIButton>();
			component.districtTexture = item.districtBackground.GetComponent<UITexture>();
			item.districtBackground.GetComponent<BtnDistrictChoose>().district = component;
			component.InitDistrict();
		}
	}

	private void OnDestroy()
	{
		MapDistrict.EndOfChangeAmountReputationAnimation -= EndOfReputationAnimationEventHandler;
		MapDistrict.EndOfAllAnimations -= ZoomIn;
		MapDistrict.StartAnimations -= ZoomOut;
		instance = null;
		Debug.Log("Destroy reputationController");
		if ((bool)TutorialManager.instance)
		{
			TutorialManager.instance.transform.SetParent(null);
		}
	}

	private void Start()
	{
		centerOnChild.Recenter();
		centerOnChild.CenterOn(centerMap);
		centerOnChild.enabled = false;
	}

	public void OnApplicationPause(bool isPause)
	{
		if (isPause)
		{
			ManagerMissions.currMission = null;
			controllerMenu.thisScript.showMainMenu();
		}
	}

	public void SetOnline(bool val)
	{
		for (int i = 0; i < districsList.Count; i++)
		{
			districsList[i].SetOnlineBtnActive(val);
		}
	}

	public void OnEnable()
	{
		StartCheckNetwork();
		mapBackground.SetActive(false);
		StartCoroutine(CheckDistrics());
		if (_camera != null)
		{
			_camera.enabled = false;
		}
		levelBar.fillAmount = 0f;
		levelLable.text = LocalizationManager.GetTermTranslation("Lobby_LevelUp/3d_map_level") + " " + 1;
		ExpHUD.instance.gameObject.SetActive(false);
	}

	public void OnDisable()
	{
		StopCoroutine(CheckDistrics());
		if (cns != null)
		{
			StopCoroutine(cns);
			cns = null;
		}
		if (_camera != null)
		{
			_camera.enabled = true;
		}
		ExpHUD.instance.gameObject.SetActive(true);
	}

	public IEnumerator CheckDistrics()
	{
		yield return null;
		while (true)
		{
			if (ReputationInterfaceController.UpdateInfo != null)
			{
				ReputationInterfaceController.UpdateInfo(false);
			}
			int cCount = 0;
			_changeDistrictState = false;
			foreach (string element in districsInfos.Keys)
			{
				if (districsInfos[element].wasCaptured || districsInfos[element].wasLosted)
				{
					_changeDistrictState = true;
					break;
				}
			}
			yield return new WaitForSeconds(180f);
		}
	}

	private void EndOfReputationAnimationEventHandler(string nameScene)
	{
		if (_changeDistrictState)
		{
			foreach (MapDistrict districs in districsList)
			{
				if (districs.isReputationAnimationInProgres)
				{
					return;
				}
			}
			if (ReputationInterfaceController.ShowNewCaptured != null)
			{
				ReputationInterfaceController.ShowNewCaptured();
			}
			_changeDistrictState = false;
		}
		else
		{
			ZoomIn();
		}
	}

	public void ShowNoInternet()
	{
		if (messageNoInternet != null)
		{
			messageNoInternet.SetActive(true);
			Invoke("HideNoInternet", 2f);
		}
	}

	public void HideNoInternet()
	{
		if (messageNoInternet != null)
		{
			messageNoInternet.SetActive(false);
		}
	}

	public void ShowMapUnAvailable()
	{
		if (messageMapUnAvailable != null)
		{
			messageMapUnAvailable.SetActive(true);
			Invoke("HideMapUnAvailable", 2f);
		}
	}

	public void HideMapUnAvailable()
	{
		if (messageMapUnAvailable != null)
		{
			messageMapUnAvailable.SetActive(false);
		}
	}

	public IEnumerator CheckNetworkStatus()
	{
		yield return new WaitForSeconds(2f);
		while (true)
		{
			if (disconnect != null)
			{
				disconnect.SetActive(!PhotonNetwork.connected);
			}
			if (ReputationInterfaceController.networkAvailable != null)
			{
				ReputationInterfaceController.networkAvailable(PhotonNetwork.connected);
			}
			if (!PhotonNetwork.connected)
			{
				controllerConnectPhoton.thisScript._initializeWorldwide();
			}
			yield return new WaitForSeconds(5f);
		}
	}

	public void StartCheckNetwork()
	{
		if (cns == null)
		{
			cns = CheckNetworkStatus();
			StartCoroutine(cns);
		}
	}

	public void ZoomIn()
	{
		if (isZoomOut)
		{
			HOTween.To(map.transform, 0.5f, new TweenParms().Prop("localScale", Vector3.one));
			isZoomOut = false;
			scrollView.enabled = true;
		}
	}

	public void ZoomOut()
	{
		if (!isZoomOut)
		{
			centerOnChild.CenterOn(centerMap);
			scrollView.enabled = false;
			HOTween.To(map.transform, 0.5f, new TweenParms().Prop("localScale", Vector3.one * zoomOnAnimationFactor));
			isZoomOut = true;
		}
	}
}
